package com.gitee.ywj1352.gasc.security;

import com.gitee.ywj1352.gasc.pojo.dto.UserDto;
import com.gitee.ywj1352.gasc.service.IUserService;
import com.gitee.ywj1352.gasc.pojo.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private IUserService userService;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        log.info("do MyUserDetailsService#loadUserByUsername and email is {}",s);
        if (!"yangwenjie.a@yuewen.com".equals(s)){
            throw new UsernameNotFoundException("没有email");
        }
        //todo 需要自己去db 查询
        UserDto login = userService.login(s);
        MyUserDetails userInfo = new MyUserDetails();
        userInfo.setUsername(login.getUsername());
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        userInfo.setAuthorities(authorities);
        return userInfo;
    }
}
